<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .a {
            width: 100px;
            height: 100px;
            background-color: #f00;
            position: absolute;
            cursor: move;
            /* display: none; */
            visibility: hidden;
        }

        .box {
            width: 500px;
            height: 500px;
            background-color: #000;
            overflow: hidden;
            position: relative;
            margin: 20px auto;
        }
    </style>
</head>

<body>

    <div class="box">
        <div class="a"></div>
    </div>


    <script>


        // display:none   以后  js获取不到他的宽高 


        var oA = document.querySelector('.a');
        var oBox = document.querySelector('.box');
        var r = oA.offsetWidth / 2;
        console.log(r);


        oBox.onmouseover = function () {
            oA.style.visibility = 'visible';
        }
        oBox.onmouseout = function () {
            oA.style.visibility = 'hidden';
        }


        oBox.onmousemove = function (e) {
            var x = e.clientX - r - oBox.offsetLeft;
            var y = e.clientY - r - oBox.offsetTop;
            oA.style.left = x + 'px';
            oA.style.top = y + 'px';
        }

    </script>

</body>

</html>